package com.hlhj.carview.aty

import android.text.Editable
import android.text.TextWatcher
import android.widget.Toast
import com.hlhj.carview.BaseAty
import com.hlhj.carview.Iview.ReportView
import com.hlhj.carview.R
import com.hlhj.carview.Toast
import com.hlhj.carview.bean.MyBaseBean
import com.hlhj.carview.http.CustomAction
import com.hlhj.carview.http.MyJsonConvert
import com.hlhj.carview.http.Urls
import com.hlhj.carview.presenter.ReportPresenter
import com.hlhj.carview.utils.ToastUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.lzy.okrx2.adapter.ObservableResponse
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.aty_report.*
import kotlinx.android.synthetic.main.lo_pubhead.*

/**
 * Created by Never Fear   on 2018\12\24 0024.
Never More....
 */
class ReportAty :BaseAty<ReportView,ReportPresenter>(), ReportView {
    override fun setResult(myBaseBean: MyBaseBean) {
        ToastUtil.toast("反馈成功",this)
        finish()

    }

    override fun initView() {
        tvTittle.text="意见反馈"


    }

    override fun logicStart() {
    }

    override fun setListener() {
        etContent.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(s: Editable?) {

                tvNum.text="${s?.length}/500"
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })

        btCommit.setOnClickListener {
            if (etContent.text.isEmpty()){
                ToastUtil.toast("不能为空",this)
                return@setOnClickListener
            }
            OkGo.post<MyBaseBean>(Urls.PubUrl)
                    .headers("type","1")
                    .params("uriPath","reportList")
                    .params("content",etContent.text.toString())
                    .converter(object : MyJsonConvert<MyBaseBean>(){})
                    .adapt(ObservableResponse<MyBaseBean>())
                    .subscribeOn(Schedulers.io())
                    .subscribe(object :CustomAction<Response<MyBaseBean>>(this){
                        override fun httpSuccess(t: Response<MyBaseBean>) {
                            "提交成功，谢谢您的反馈".Toast()
                            finish()
                        }
                    })
        }
    }

    override fun bindView(): ReportView {
        return this
    }

    override fun createPresenter(): ReportPresenter {
        return ReportPresenter()
    }

    override fun getContentID(): Int {
        return R.layout.aty_report
    }
}